﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ComputerArchitecture.Numbers
{
    /// <summary>
    /// Represent signed number in complement code
    /// </summary>
    public class SignedBitNumber : BitNumber, ISignedBitNumber
    {
        public int Sign
        {
            get { return SignBit.IsSet ? -1 : 1; }
        }

        public Bit SignBit
        {
            get { return this.Bits[0]; }
        }

        #region Constructors

        public SignedBitNumber(int count) : base(count)
        {
        }

        public SignedBitNumber(IEnumerable<Bit> bits) : base(bits)
        {
        }

        #endregion

        #region Methods

        public override void Expand(int newSize)
        {
            int expandSize = newSize - this.Size;
            for (int i = 0; i < expandSize; ++i)
            {
                this.Bits.Add(Bit.Zero);
            }
            Bit carryBit;
            this.ArithmeticShiftRight(expandSize, out carryBit);
        }

        public override object Clone()
        {
            return new SignedBitNumber(this.Bits);
        }

        public bool Equals(ISignedBitNumber other)
        {
            return this.Equals((IBitNumber) other);
        }

        public static SignedBitNumber GetZero(int size)
        {
            return new SignedBitNumber(size);
        }

        public static SignedBitNumber GetOne(int size)
        {
            SignedBitNumber number = new SignedBitNumber(size);
            number.Bits.Last().Set();
            return number;
        }

        public static SignedBitNumber GetMin(int size)
        {
            SignedBitNumber number = new SignedBitNumber(size);
            number.Bits.First().Set();
            return number;
        }

        #endregion
    }
}
